import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Properties;

/**
 * 工具类
 */
public class Util {
    /**
     * 随机产生一个整数
     *
     * @param start 开始范围
     * @param end   结束范围
     * @return 返回随机数
     */
    public static int random(int start, int end) {
        return (int) (Math.random() * (end - start + 1) + start);
    }


    /**
     * 将货币数字以千分位的方式表示
     * “.”后面两个零表示保留两位小数，一个零表示保留一位小数
     *
     * @param d 输入的货币值
     * @return 一个字符串
     */
    public static String currency(double d) {
        DecimalFormat decimalFormat = new DecimalFormat("###,###.00");
        return decimalFormat.format(d);
    }

    /**
     * 驼峰名转蛇形名
     *
     * @param str 输入的驼峰名
     * @return 返回一个蛇形名
     */
    public static String camelToSnake(String str) {
        String str2 = "";

        String[] str1 = str.split("");
        for (int i = 0; i < str1.length; i++) {
            if (i == 0) {
                continue;
            }
            char[] ch = str1[i].toCharArray();

            if (Character.isUpperCase(ch[0])) {
                str1[i] = "_" + str1[i];
            }
        }
        for (String items : str1) {
            str2 += items;

        }
        return str2.toLowerCase();
    }


    /**
     * 蛇形名转驼峰名
     *
     * @param str 输入的蛇形名
     * @return 返回驼峰名
     */
    public static String snakeToCamel(String str) {
        String str2 = "";
        String[] str1 = str.split("");
        for (int i = 0; i < str1.length; i++) {
            if (str1[i].equals("_")) {
                str1[i] = "";
                str1[i + 1] = str1[i + 1].toUpperCase();
            }
        }
        for (String items : str1) {
            str2 += items;

        }
        return str2;
    }

    /**
     * 格式化日期，用年月日输出
     *
     * @param dateStr
     * @return
     */
    public static String formatDate(String dateStr) {
        LocalDate date = LocalDate.parse(dateStr);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月d日");
        String text = date.format(formatter);
        return text;

    }

    /**
     * 调用图片，返回一个Image
     *
     * @param path 图片地址
     * @return
     */
    public static Image getImage(String path) {
        return new ImageIcon(Util.class.getResource(path)).getImage();
    }

    /**
     * 调用图片，返回一个ImageIcon
     *
     * @param path 图片地址
     * @return
     */
    public static ImageIcon getIcon(String path) {
        return new ImageIcon(Util.class.getResource(path));
    }

    /**
     * 序列化对象
     *
     * @param path 保存序列化对象的路径
     * @param obj  序列化对象
     */
    public static void writeObject(String path, Object obj) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream(path);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 反序列化对象
     *
     * @param path 反序列化对象路径
     * @return 返回对象
     */
    public static Object readObject(String path) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(path);
            ois = new ObjectInputStream(fis);
            return (Object) ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 剪切文件
     *
     * @param src  目标文件路径
     * @param dist 目标目录路径
     */
    public static void moveFile(String src, String dist) {
        String[] strs = src.split("/");
        File file1 = new File(src);
        File file2 = new File(dist);
        if (!file1.exists() || !file2.exists()) {
            System.out.println("目标文件或目录不存在");
            return;
        }
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(src);
            fos = new FileOutputStream(dist + "/" + strs[strs.length - 1]);
            int len = 0;
            int d = 0;
            while (fis.read() != -1) {
                fos.write(fis.read());
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        file1.delete();

    }

    /**
     * 自动保存成Properties
     *
     * @param pro  集合
     * @param path 保存的文件地址和名字
     */
    public static void storeProperties(Properties pro, String path) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(path);
            pro.store(fos, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 自动读取Properties文件
     * @param path  文件地址
     * @return   返回一个Properties集合
     */
    public static Properties loadProperties(String path) {
        Properties properties = new Properties();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(path);
            properties.load(fis);
            return properties;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

}
